package com.kingsalert.kingsalert.dialog;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.ViewGroup;

import androidx.annotation.NonNull;

import com.kingsalert.kingsalert.R;
import com.kingsalert.kingsalert.activity.LoginActivity;
import com.kingsalert.kingsalert.config.UserState;
import com.kingsalert.kingsalert.util.UserInfoUtil;
import com.kingsalert.kingsalert.util.http.HttpUtil;


public class LogoutDialog extends Dialog {

    private final String page = "DIALOG_LOGOUT";

    public LogoutDialog(@NonNull Context context) {
        super(context);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_logout);

        getWindow().setBackgroundDrawableResource(R.color.transparent);
        setCanceledOnTouchOutside(true);
        getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

        initView();
    }

    @SuppressLint("SetTextI18n")
    private void initView(){

        findViewById(R.id.dlo_tv_cancel).setOnClickListener(v -> {
            HttpUtil.getInstance().saveBuriedPoint(page,"CANCEL_CLICK","");
            dismiss();
        });

        findViewById(R.id.dlo_tv_logout).setOnClickListener(v -> {

            HttpUtil.getInstance().saveBuriedPoint(page,"LOGOUT_CLICK","");

            UserInfoUtil.getInstance().clear();
            UserState.clearState();

            dismiss();

            Intent intent = new Intent(getContext(), LoginActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
            getContext().startActivity(intent);

        });

    }

    @Override
    public void show() {
        super.show();
        HttpUtil.getInstance().saveBuriedPoint(page,"START","");
    }

    @Override
    public void dismiss() {
        super.dismiss();
        HttpUtil.getInstance().saveBuriedPoint(page,"END","");

    }
}
